home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / comm / ytsg3.zip / WCMATCH.C < prev    next >
Text File  |  1991-12-03  |  2KB  |  74 lines

  1. /* match two strings: both may be ambiguous in the DOS (OS/2) sense: */
  2. /* using a question mark for a place holder, and a * to mean "up to */
  3. /* the next dot" */
  4.  
  5. /* returns 1 for match, 0 for not */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10.  
  11. #define NEXTDOT(p) for(; *p && (*p != '.'); p++)
  12. #define MATCH   1
  13. #define NOMATCH 0
  14.  
  15. wcmatch(char *string1, char *string2)
  16. {
  17.    char *p, *q;
  18.    char *allfs = "*.*";
  19.  
  20.    if (!strcmp(string1, allfs) || !strcmp(string2, allfs))
  21.      return(MATCH);
  22.    if (!strcmp(string1, "*")   || !strcmp(string2, "*"))
  23.      return(MATCH);
  24.  
  25.    for (p = string1, q = string2;
  26.         *p && *q;
  27.         p++, q++)
  28.      {
  29.        if ((*p == '?') || (*q == '?'))
  30.          continue;
  31.        else
  32.          if ((*p == '*') || (*q == '*'))
  33.            {
  34.              NEXTDOT(p); NEXTDOT(q);
  35.              if (!*p && !*q)
  36.                return(MATCH);
  37.              else
  38.                if (!*p || !*q)
  39.                  return(NOMATCH);
  40.            }
  41.          else
  42.            if (toupper(*p) != toupper(*q))
  43.              return(NOMATCH);
  44.      }
  45.      if (toupper(*p) == toupper(*q))
  46.        return(MATCH);
  47.      else
  48.        if (*p == '*')
  49.          NEXTDOT(p);
  50.        else
  51.          if (*q == '*')
  52.            NEXTDOT(q);
  53.        if (toupper(*p) == toupper(*q))
  54.          return(MATCH);
  55.      return(NOMATCH);
  56. }
  57.  
  58. #ifdef DEBUG
  59. main(argc, argv, envp)
  60.    int argc;
  61.    char *argv[];
  62.    char *envp[];
  63. {
  64.    if (argc < 3)
  65.      printf("\nInvoke as wcmatch string1 string2\n");
  66.    else
  67.      if (wcmatch(argv[1], argv[2]))
  68.        printf("\n%s  matches  %s\n", argv[1], argv[2]);
  69.      else
  70.        printf("\n%s  does NOT match  %s\n", argv[1], argv[2]);
  71.    return(0);
  72. }
  73. #endif
  74.